package com.financing.wap.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.ResultMap;
import com.financing.wap.dao.IAccountDao;
import com.financing.wap.dao.IAccountWithdrawDao;
import com.financing.wap.dto.AccountCorpDTO;
import com.financing.wap.dto.AccountWithdrawDTO;
import com.financing.wap.service.IAccountWithdrawService;
import com.financing.wap.util.DTOdateSetUtil;

@Service("accountWithdrawServiceImpl")
public class AccountWithdrawServiceImpl implements IAccountWithdrawService{

	@Autowired
	private IAccountWithdrawDao accountWithdrawDaoImpl;
	@Autowired
	private IAccountDao accountDao;
	
	Logger logger = Logger.getLogger(getClass());
	@Override
	public List<AccountWithdrawDTO> queryAccountWithdrawList(
			AccountWithdrawDTO accountWithdrawDTO) throws SQLException {
		return accountWithdrawDaoImpl.queryAccountWithdrawList(accountWithdrawDTO);
	}

	@Override
	public int updateAccountWithdraw(AccountWithdrawDTO accountWithdrawDTO)
			throws SQLException {
		return accountWithdrawDaoImpl.updateAccountWithdraw(accountWithdrawDTO);
	}

	@Override
	public String insertAccountWithdraw(AccountWithdrawDTO accountWithdrawDTO)
			throws SQLException {
		
		logger.info("In AccountWithdrawServiceImpl insertAccountWithdraw getUserAccount");
		//获取账户信息
		AccountCorpDTO accountBean = accountDao.getFaAccountByUserId(accountWithdrawDTO.getUserId());
		
		//可提现额度判断
		BigDecimal withdrawAmount = accountWithdrawDTO.getWithdrawAmount();
		
		if(withdrawAmount.compareTo(accountBean.getAccountBalanceActivity()) >0){
			
			return ResultMap.STATUS_CODE_WITHDRAW_AMOUNT_TOO_BIG;
		}
		logger.info("In AccountWithdrawServiceImpl insertAccountWithdraw updateAccountBalanceActivity");
		//修改账户可提现余额-添加冻结金额
		AccountCorpDTO accountDto =  new AccountCorpDTO();
		DTOdateSetUtil.setUpdateInfo(accountDto);
		accountDto.setId(accountBean.getId());
		accountDto.setAccountBalanceActivity(accountBean.getAccountBalanceActivity().subtract(withdrawAmount));// 可提现金额-提现金额
		accountDto.setAccountBalanceSleep(accountBean.getAccountBalanceSleep().add(withdrawAmount));//冻结金额+可提现金额
		accountDao.updateAccountBalanceActivity(accountDto);
		
		logger.info("In AccountWithdrawServiceImpl insertAccountWithdraw insertAccountWithdraw");
		//添加提现表
		accountWithdrawDTO.setWithdrawStatus("01");
		accountWithdrawDaoImpl.insertAccountWithdraw(accountWithdrawDTO);
		
		
		
		return ResultMap.STATUS_CODE_SUCCESS;
	}

}
